package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author binYan
 */
@RestController
@RequestMapping("/VIID/System")
public class ViidSystemController {

    Logger LOGGER = LogManager.getLogger(ViidSystemController.class);

    /**
     * 注册接口
     *
     * @param request 请求
     * @return 返回
     */
    @RequestMapping(value = "/Register", produces = "application/VIID+JSON;charset=utf-8")
    public GATResponseVO register(@RequestBody RegisterDTO dto, HttpServletRequest request) {
        GATResponseVO vo = new GATResponseVO();
        try {

            LOGGER.info("注册的设备信息：{}", JSONObject.toJSONString(dto));
            String requestURI = request.getRequestURI();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            ResponseStatusObjectDTO objectDTO = new ResponseStatusObjectDTO();
            objectDTO.setId(dto.getRegisterObject().getDeviceID());
            objectDTO.setLocalTime(sdf.format(System.currentTimeMillis()));
            objectDTO.setRequestURL(requestURI);
            objectDTO.setStatusCode(0);
            objectDTO.setStatusString("OK");
            vo.setResponseStatusObject(objectDTO);
        } catch (Exception e) {
            LOGGER.error("注销错误{}", e);
        }
        return vo;
    }

    /**
     * 保活接口
     *
     * @return 返回
     */
    @RequestMapping(value = "/Keepalive", method = RequestMethod.POST, produces = "application/VIID+JSON;charset=utf-8")
    public GATResponseVO keepalive(@RequestBody KeepaliveDTO dto, HttpServletRequest request) {
        GATResponseVO vo = new GATResponseVO();
        try {
            LOGGER.info("对方发送的心跳的信息：{}", JSONObject.toJSONString(dto));
            String requestURI = request.getRequestURI();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            ResponseStatusObjectDTO objectDTO = new ResponseStatusObjectDTO();
            objectDTO.setId(dto.getKeepaliveObject().getDeviceID());
            objectDTO.setLocalTime(sdf.format(System.currentTimeMillis()));
            objectDTO.setRequestURL(requestURI);
            objectDTO.setStatusCode(0);
            objectDTO.setStatusString("OK");
            vo.setResponseStatusObject(objectDTO);
        } catch (Exception e) {
            LOGGER.error("注销错误{}", e);
        }
        return vo;
    }

    /**
     * 注销设备
     *
     * @return 返回
     */
    @RequestMapping(value = "/UnRegister", produces = "application/VIID+JSON;charset=utf-8")
    public GATResponseVO unRegister(@RequestBody UnRegisterDTO dto, HttpServletRequest request) {
        // 构造返回实体
        GATResponseVO vo = new GATResponseVO();
        try {
            LOGGER.info("获取的注销的请求参数：{}", JSONObject.toJSONString(dto));

            String requestURI = request.getRequestURI();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            // 注销就是让设备下线
            ResponseStatusObjectDTO objectDTO = new ResponseStatusObjectDTO();
            objectDTO.setId(dto.getUnRegisterObject().getDeviceID());
            objectDTO.setLocalTime(sdf.format(System.currentTimeMillis()));
            objectDTO.setRequestURL(requestURI);
            objectDTO.setStatusCode(0);
            objectDTO.setStatusString("OK");
            vo.setResponseStatusObject(objectDTO);
        } catch (Exception e) {
            LOGGER.error("注销错误{}", e);
        }
        return vo;
    }

    /**
     * 校时接口
     *
     * @return 返回
     */
    @RequestMapping(value = "/Time", method = RequestMethod.GET, produces = "application/VIID+JSON;charset=utf-8")
    public String time() {
        // 构造返回的参数
        JSONObject result = new JSONObject();
        // 构造参数
        JSONObject res = new JSONObject();
        // 服务器标识
        res.put("VIIDServerID", "1506210001");
        /**
         * 校时模式
         * 1、网络
         * 2、手动
         */
        res.put("TimeMode", 2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        // 日期时间
        res.put("LocalTime", new Date());
        // 时区
        res.put("TimeZone", TimeZone.getTimeZone("Asia/Shanghai"));
        result.put("SystemTimeObject", res);
        LOGGER.info("打印校时返回的参数：{}", result.toJSONString());
        return result.toJSONString();
    }
}
